//
//  MyGroupTextCell.m
//  Yiqidianyin
//
//  Created by apple on 15/8/29.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MyGroupTextCell.h"

@interface MyGroupTextCell ()
@property (strong, nonatomic) IBOutlet UILabel *label;
@end

@implementation MyGroupTextCell

-(void)setCellInfoWithNSDictionary:(NSDictionary *)dic{
    
    NSString * str = dic[@"msg"];
    
    [self attributedTextWithText:str];
}

-(void)attributedTextWithText:(NSString *)str{
    
    //找到本地emoticons.plist文件
    NSString * path = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
    //将文件中的内容读取出来存入数组中
    NSArray * faces = [NSArray arrayWithContentsOfFile:path];
    //存储图片二进制数据的数组
    NSMutableArray * faceImageData = [NSMutableArray arrayWithCapacity:faces.count];
    //将图片名字对应的图片转为二进制数据存入数组
    for (int i = 0; i< faces.count; i++)
    {
        NSDictionary * dic = faces[i];
        NSString * imageName = dic[@"chs"];
        UIImage * image = [UIImage imageNamed:dic[@"png"]];
        
        NSData * imageData = UIImagePNGRepresentation(image);
        
        NSDictionary * dic1 = @{@"name":imageName,@"data":imageData};
        
        [faceImageData addObject:dic1];
        
    }
    
    //遍历字符串，将字符串中符合"[XX]"格式的文本取出来放入数组
    NSArray * result = [self tttString:str];
    
    NSMutableAttributedString * textString = [[NSMutableAttributedString alloc]initWithString:str];
    //反向遍历数组，将"[XX]"替换为图片
    for (NSInteger i = result.count -1; i>=0; i--)
    {
        //检查字符串的结果
        NSTextCheckingResult * r = result[i];
        //将"[XX]"取出来
        NSString * tempString = [str substringWithRange:r.range];
        //遍历表情数组，看是否有与"[XX]"对应的图片的名字，如果有，则将图片替换"[XX]"
        for (int j = 0; j<faces.count; j++)
        {
            //取出字典
            NSDictionary * dic = faces[j];
            NSString * dicName = dic[@"chs"];
            //判断"[XX]"和字典中图片对应的名字是否相同，相同则将图片放入属性字符串的附文本中，替换"[XX]"
            if ([tempString isEqualToString:dicName])
            {
                //取出图片
                NSString * imageName = dic[@"png"];
                
                UIImage * image = [UIImage imageNamed:imageName];
                //放入附文本
                NSTextAttachment * attachment = [[NSTextAttachment alloc]init];
                attachment.image = image;
                //给属性字符串赋值
                NSAttributedString * imageString = [NSAttributedString attributedStringWithAttachment:attachment];
                //替换
                [textString replaceCharactersInRange:r.range withAttributedString:imageString];
                // break;
            }
        }
    }
    //显示在label上
    self.label.attributedText = textString;
}

//传入一个字符串，返回字符串中符合"[XX]"的数组
-(NSArray *)tttString:(NSString *)str
{
    //匹配 [中文文字]
    NSString * pattern = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
    //创建正则表达式对象NSRegularExpression
    NSError * error;
    NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
    if (!re)
    {
    }
    //通过正则表达式对象，对字符串进行匹配
    NSArray * arr = [re matchesInString:str options:0 range:NSMakeRange(0, str.length)];
    
    return arr;
    
    //数组中每一个元素是NSTextCheckingResult对象
    // NSTextCheckingResult * r;
    //NSRange range = r.range;
    
    
}


@end
